package com.hskn.hss.utils.location;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import java.util.ArrayList;
import java.util.List;

public class GPSConverterUtils {

    public static final String BAIDU_LBS_TYPE = "bd09ll";
    public static double pi = 3.1415926535897932384626;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    public static void main(String[] args) {
        gg();
    }
    public static void GDTogps84(){
        //---高德地图返回的处理完轨迹偏移的坐标集合
        String gdPoints = "{\n" +
                "    \"data\": {\n" +
                "        \"counts\": 1,\n" +
                "        \"tracks\": [\n" +
                "            {\n" +
                "                \"counts\": 19,\n" +
                "                \"degradedParams\": {},\n" +
                "                \"distance\": 238,\n" +
                "                \"endPoint\": {\n" +
                "                    \"locatetime\": 1630374142000,\n" +
                "                    \"location\": \"119.152626,36.697495\"\n" +
                "                },\n" +
                "                \"points\": [\n" +
                "                    {\n" +
                "                        \"locatetime\": 1630374130000,\n" +
                "                        \"location\": \"119.15004,36.696972\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374130000,\n" +
                "                        \"location\": \"119.15003967285156,36.69697189331055\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"0\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"locatetime\": 1630374130405,\n" +
                "                        \"location\": \"119.150101,36.696983\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"locatetime\": 1630374130486,\n" +
                "                        \"location\": \"119.150116,36.696987\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374130486,\n" +
                "                        \"location\": \"119.150241,36.697013\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"1\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374130486,\n" +
                "                        \"location\": \"119.150392,36.697043\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"0\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374130486,\n" +
                "                        \"location\": \"119.150545,36.697074\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"1\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374130486,\n" +
                "                        \"location\": \"119.150697,36.697105\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"0\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"locatetime\": 1630374134578,\n" +
                "                        \"location\": \"119.150734,36.697113\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374134578,\n" +
                "                        \"location\": \"119.150888,36.697144\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"1\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374134578,\n" +
                "                        \"location\": \"119.151091,36.697186\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"0\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374134578,\n" +
                "                        \"location\": \"119.151294,36.697227\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"1\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374134578,\n" +
                "                        \"location\": \"119.151497,36.697269\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"0\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374134578,\n" +
                "                        \"location\": \"119.1517,36.697311\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"1\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"locatetime\": 1630374139705,\n" +
                "                        \"location\": \"119.151766,36.697324\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"locatetime\": 1630374139705,\n" +
                "                        \"location\": \"119.151764,36.697323\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374139705,\n" +
                "                        \"location\": \"119.15202,36.697374\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"0\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374139705,\n" +
                "                        \"location\": \"119.152399,36.697449\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"1\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"accuracy\": 550.0,\n" +
                "                        \"direction\": 511.0,\n" +
                "                        \"locatetime\": 1630374142000,\n" +
                "                        \"location\": \"119.152626,36.697495\",\n" +
                "                        \"props\": {\n" +
                "                            \"danger\": \"0\"\n" +
                "                        },\n" +
                "                        \"speed\": 255.0\n" +
                "                    }\n" +
                "                ],\n" +
                "                \"startPoint\": {\n" +
                "                    \"locatetime\": 1630374130000,\n" +
                "                    \"location\": \"119.15004,36.696972\"\n" +
                "                },\n" +
                "                \"time\": 14120,\n" +
                "                \"trid\": 80,\n" +
                "                \"trname\": \"测试轨迹2\"\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"errcode\": 10000,\n" +
                "    \"errdetail\": null,\n" +
                "    \"errmsg\": \"OK\"\n" +
                "}";
        StringBuffer buffer = new StringBuffer();
        JSONObject jsonObject = JSONObject.parseObject(gdPoints);
        JSONObject data = (JSONObject)jsonObject.get("data");
        JSONArray tracks = (JSONArray)data.get("tracks");
        JSONObject track = (JSONObject)tracks.get(0);
        JSONObject endPoint = (JSONObject)track.get("endPoint");
        JSONObject startPoint = (JSONObject)track.get("startPoint");
        JSONArray points = (JSONArray)track.get("points");

        String lnglat = startPoint.getString("location");
        String[] ll = lnglat.split(",");
        GPS gps = gcj_To_Gps84(Double.parseDouble(ll[1]),Double.parseDouble(ll[0]));
        buffer.append("points.push(new T.LngLat("+gps.getLon()+","+gps.getLat()+"));\n");
        for (Object point : points) {
            JSONObject pp = (JSONObject)point;
            String lnglat2 = pp.getString("location");
            String[] ll2 = lnglat2.split(",");
            GPS gps2 = gcj_To_Gps84(Double.parseDouble(ll2[1]),Double.parseDouble(ll2[0]));
            buffer.append("points.push(new T.LngLat("+gps2.getLon()+","+gps2.getLat()+"));\n");
        }
        String lnglat3 = endPoint.getString("location");
        String[] ll3 = lnglat3.split(",");
        GPS gps3 = gcj_To_Gps84(Double.parseDouble(ll3[1]),Double.parseDouble(ll3[0]));
        buffer.append("points.push(new T.LngLat("+gps3.getLon()+","+gps3.getLat()+"));\n");

        System.out.println(buffer.toString());
    }
    public static void gg(){
        String ss = "[{\"deviceType\":\"CAR_LOCATION\",\"no\":\"14146284901\",\"state\":1,\"content\":{\"type\":\"LOCATION_REPORT\",\"info\":{\"oilMass\":46.63,\"dateTime\":\"2021-09-02T04:01:06\",\"altitude\":0,\"location\":\"36.705500,119.149632\",\"speed\":370,\"status\":\"0\",\"direction\":\"90\",\"mileage\":51146.0}}},\n" +
                "{\"deviceType\":\"CAR_LOCATION\",\"no\":\"14146284901\",\"state\":1,\"content\":{\"type\":\"LOCATION_REPORT\",\"info\":{\"oilMass\":46.63,\"dateTime\":\"2021-09-02T04:01:16\",\"altitude\":0,\"location\":\"36.705568,119.150752\",\"speed\":290,\"status\":\"0\",\"direction\":\"88\",\"mileage\":51147.0}}},\n" +
                "{\"deviceType\":\"CAR_LOCATION\",\"no\":\"14146284901\",\"state\":1,\"content\":{\"type\":\"LOCATION_REPORT\",\"info\":{\"oilMass\":46.55,\"dateTime\":\"2021-09-02T04:01:26\",\"altitude\":0,\"location\":\"36.705568,119.151088\",\"speed\":0,\"status\":\"0\",\"direction\":\"90\",\"mileage\":51148.0}}},\n" +
                "{\"deviceType\":\"CAR_LOCATION\",\"no\":\"14146284901\",\"state\":1,\"content\":{\"type\":\"LOCATION_REPORT\",\"info\":{\"oilMass\":46.55,\"dateTime\":\"2021-09-02T04:01:36\",\"altitude\":0,\"location\":\"36.705568,119.151088\",\"speed\":0,\"status\":\"0\",\"direction\":\"90\",\"mileage\":51148.0}}},\n" +
                "{\"deviceType\":\"CAR_LOCATION\",\"no\":\"14146284901\",\"state\":1,\"content\":{\"type\":\"LOCATION_REPORT\",\"info\":{\"oilMass\":46.55,\"dateTime\":\"2021-09-02T04:01:46\",\"altitude\":0,\"location\":\"36.705580,119.151200\",\"speed\":100,\"status\":\"0\",\"direction\":\"86\",\"mileage\":51148.0}}},\n" +
                "{\"deviceType\":\"CAR_LOCATION\",\"no\":\"14146284901\",\"state\":1,\"content\":{\"type\":\"LOCATION_REPORT\",\"info\":{\"oilMass\":46.55,\"dateTime\":\"2021-09-02T04:01:51\",\"altitude\":0,\"location\":\"36.705664,119.151400\",\"speed\":170,\"status\":\"0\",\"direction\":\"40\",\"mileage\":51148.0}}},\n" +
                "{\"deviceType\":\"CAR_LOCATION\",\"no\":\"14146284901\",\"state\":1,\"content\":{\"type\":\"LOCATION_REPORT\",\"info\":{\"oilMass\":46.55,\"dateTime\":\"2021-09-02T04:01:54\",\"altitude\":0,\"location\":\"36.705820,119.151520\",\"speed\":240,\"status\":\"0\",\"direction\":\"18\",\"mileage\":51148.0}}},\n" +
                "{\"deviceType\":\"CAR_LOCATION\",\"no\":\"14146284901\",\"state\":1,\"content\":{\"type\":\"LOCATION_REPORT\",\"info\":{\"oilMass\":46.55,\"dateTime\":\"2021-09-02T04:01:56\",\"altitude\":0,\"location\":\"36.705932,119.151568\",\"speed\":270,\"status\":\"0\",\"direction\":\"1\",\"mileage\":51148.0}}}]\n";
        JSONArray array = JSONObject.parseArray(ss);
        List list = new ArrayList<>();

        Integer danger = 1;
        for (Object o : array) {
            JSONObject obj = (JSONObject)o;
            JSONObject content = (JSONObject)obj.get("content");
            JSONObject info = (JSONObject)content.get("info");
            String location = info.getString("location");
            String[] aa = location.split(",");
            double lat = Double.parseDouble(aa[0]);
            double lon = Double.parseDouble(aa[1]);
            //84坐标转为高德坐标
            GPS gps = gps84_To_Gcj02(lat,lon);
            String lnglat = gps.getLon()+","+gps.getLat();
            JSONObject obj2 = new JSONObject();
            obj2.put("location",lnglat);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            danger = danger==1?0:1;
            obj2.put("locatetime",System.currentTimeMillis());
            JSONObject o2 = new JSONObject();
            o2.put("danger",danger+"");
            obj2.put("props",o2);
            list.add(obj2);
        }
        System.out.println(JSONObject.toJSONString(list));
    }
    public static void gps84toGD(){
        //网格的坐标点集合转换为高德坐标集合
        //84坐标
        String ss = "[{\"lng\":119.14419,\"lat\":36.69626},{\"lng\":119.14438,\"lat\":36.6963},{\"lng\":119.14433,\"lat\":36.69648},{\"lng\":119.14456,\"lat\":36.69653},{\"lng\":119.14462,\"lat\":36.69634},{\"lng\":119.14488,\"lat\":36.69639},{\"lng\":119.14482,\"lat\":36.69658},{\"lng\":119.14511,\"lat\":36.69663},{\"lng\":119.14515,\"lat\":36.69644},{\"lng\":119.14566,\"lat\":36.69657},{\"lng\":119.14575,\"lat\":36.69688},{\"lng\":119.14656,\"lat\":36.69698},{\"lng\":119.14662,\"lat\":36.6973},{\"lng\":119.14747,\"lat\":36.6972},{\"lng\":119.14787,\"lat\":36.69705}]";
        JSONArray array = JSONObject.parseArray(ss);
        List list = new ArrayList<>();

        Integer danger = 1;
        for (Object o : array) {
            JSONObject obj = (JSONObject)o;
            double lat = Double.parseDouble(obj.getString("lat"));
            double lon = Double.parseDouble(obj.getString("lng"));
            //84坐标转为高德坐标
            GPS gps = gps84_To_Gcj02(lat,lon);
            String lnglat = gps.getLon()+","+gps.getLat();
            JSONObject obj2 = new JSONObject();
            obj2.put("location",lnglat);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            danger = danger==1?0:1;
            obj2.put("locatetime",System.currentTimeMillis());
            JSONObject o2 = new JSONObject();
            o2.put("danger",danger+"");
            obj2.put("props",o2);
            list.add(obj2);
        }
        System.out.println(JSONObject.toJSONString(list));
    }
    /**
     * 84 to 火星坐标系 (GCJ-02)高德地图
     * @param lat
     * @param lon
     */
    public static GPS gps84_To_Gcj02(double lat, double lon) {
        if (outOfChina(lat, lon))
        {
            return null;
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new GPS(mgLat, mgLon);
    }

    /**
     * * 火星坐标系 (GCJ-02)高德地图 to 84
     * * @param lon * @param lat * @return
     */
    public static GPS gcj_To_Gps84(double lat, double lon) {
        GPS gps = transform(lat, lon);
        double lontitude = lon * 2 - gps.getLon();
        double latitude = lat * 2 - gps.getLat();
        return new GPS(latitude, lontitude);
    }

    /**
     * 将 GCJ-02 坐标转换成 BD-09百度坐标 坐标
     *
     * @param gg_lat
     * @param gg_lon
     */
    public static GPS gcj02_To_Bd09(double gg_lat, double gg_lon) {
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
        double bd_lon = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        return new GPS(bd_lat, bd_lon);
    }

    /**
     * * 将 BD-09百度坐标 坐标转换成GCJ-02 坐标 *
     * * @param
     * bd_lat * @param bd_lon * @return
     */
    public static GPS bd09_To_Gcj02(double bd_lat, double bd_lon) {
        double x = bd_lon - 0.0065;
        double y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
        double gg_lon = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new GPS(gg_lat, gg_lon);
    }

    /**
     * (BD-09)-->84
     * @param bd_lat
     * @param bd_lon
     * @return
     */
    public static GPS bd09_To_Gps84(double bd_lat, double bd_lon) {

        GPS gcj02 = bd09_To_Gcj02(bd_lat, bd_lon);
        GPS map84 = gcj_To_Gps84(gcj02.getLat(),
                gcj02.getLon());
        return map84;

    }

    /**
     * is or not outOfChina
     * @param lat
     * @param lon
     * @return
     */
    public static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    public static GPS transform(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new GPS(lat, lon);
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new GPS(mgLat, mgLon);
    }

    public static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }


    public static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return ret;
    }

}